using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Security;
using System.Security.Principal;
using Portal.Api.Statistics.Service;
using Portal.Api.Statistics;

namespace Portal 
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
    static readonly string[] _sessionStartIgnoreFiles = { "~/LifeguardData.aspx", "~/Download.aspx", "~/Feed.aspx" };

		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start(Object sender, EventArgs e)
		{
      // In .Net Framework 2.0, the AppDomain with all included threads is recycled, when a subdirectory is renamed or deleted.
      // This has a really negative impact for modules like ImageBrowser or FileBrowser...
      // The following code fixes this beaviour by turning off the monitoring from the root website directory.
      // Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy.
      // Found at: http://forums.asp.net/p/1310976/2581558.aspx
      System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
      object o = p.GetValue(null, null);
      System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase);
      object monitor = f.GetValue(o);
      System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); 



      //Portal.Api.Helper.InstallFile("Portal.config");
      //Portal.Api.Helper.InstallFile("Portal.css");
      //Portal.Api.Helper.InstallFile("PortalFooter.ascx");
      //Portal.Api.Helper.InstallFile("PortalHeader.ascx");
      //Portal.Api.Helper.InstallFile("Users.config");
      //if(Portal.Api.Helper.InstallFile("web.config"))
      //{
      //  throw new Exception("web.config file installed, reload the application");
      //}
		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{
      // If its not an ignored file, then register the Session Start as a Visit.
      bool ignoreSession = false;

      foreach (string ignoreReq in _sessionStartIgnoreFiles)
      {
        if (0 == string.Compare(Request.AppRelativeCurrentExecutionFilePath, ignoreReq, true))
        {
          ignoreSession = true;
          break;
        }
      }

      if (!ignoreSession)
      {
        RequestStatisticService service = (RequestStatisticService)Statistic.GetService(typeof(RequestStatisticService));
        service.AddVisit(Context);
      }
		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
			if(Request.IsAuthenticated)
			{
				HttpContext.Current.User = Portal.Api.UserManagement.GetUser(HttpContext.Current.User.Identity.Name);
			}
		}

		protected void Application_Error(Object sender, EventArgs e)
		{

		}

		protected void Session_End(Object sender, EventArgs e)
		{

		}

		protected void Application_End(Object sender, EventArgs e)
		{

		}
			
		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}

